import 'package:flutter/material.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter_demo/models/recommend_interface.dart';
import 'package:flutter_demo/models/user_model.dart';

import 'user_model.dart';

/// 列表模型
class ArticleList {
  /// 包含ArticleItem 模型的集合
  List<ArticleItem> list;

  // 构造方法
  ArticleList(this.list);

  /// 循环后台返回的数组 将每一项组装成 ArticleItem
  factory ArticleList.fromJson(List<dynamic> list) {
    return ArticleList(
      list.map((item) => ArticleItem.fromJson(item)).toList(),
    );
  }
}

// 每一项/详情模型
class ArticleItem implements RecommendInterface {
  final int id;
  final int userId;
  final UserItem userEdentity; // 这个是用户模型
  final List coverUrlList;
  final String title;
  final int commentCount;
  final int thumbUpCount;
  final int readCount;

  // 构造方法
  ArticleItem({
    @required this.id,
    @required this.userId,
    @required this.userEdentity,
    @required this.coverUrlList,
    @required this.title,
    @required this.commentCount,
    @required this.thumbUpCount,
    @required this.readCount,
  });

  factory ArticleItem.fromJson(dynamic item) {
    return ArticleItem(
      id: item['id'],
      userId: item['userId'],
      userEdentity: UserItem.fromJson(item['user']), // 将用户数据转为用户模型
      coverUrlList: item['coverUrlList'],
      title: item['title'],
      commentCount: item['commentCount'],
      thumbUpCount: item['thumbUpCount'],
      readCount: item['readCount'],
    );
  }
}
